﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 热处理记录Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksHeatTreatmentService), ServiceLifetime = LifeTime.Transient)]
    public class CksHeatTreatmentService : BaseService<CksHeatTreatment>, ICksHeatTreatmentService
    {
        /// <summary>
        /// 查询热处理记录列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksHeatTreatmentDto> GetList(CksHeatTreatmentQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.Id)
                .ToPage<CksHeatTreatment, CksHeatTreatmentDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksHeatTreatment GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加热处理记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksHeatTreatment AddCksHeatTreatment(CksHeatTreatment model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改热处理记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksHeatTreatment(CksHeatTreatment model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksHeatTreatment> QueryExp(CksHeatTreatmentQueryDto parm)
        {
            var predicate = Expressionable.Create<CksHeatTreatment>();
            predicate.AndIF(!parm.BatchNo.IsEmpty(),a=>a.BatchNo == parm.BatchNo);
            return predicate;
        }

        public List<CksHeatTreatmentDetails> GetDegrees(CksHeatTreatmentQueryDto parm)
        {
           return Queryable().Context.Queryable<CksHeatTreatmentDetails>()
                .Where(a=>a.Location == parm.Location && a.BatchNo == parm.BatchNo)
                .ToList();
        }
    }
}